<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
		<el-form-item label="商品名称" prop="productName">
		  <el-input v-model="queryParams.productName" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery"/>
		</el-form-item>
      <el-form-item label="采购时间" prop="enterDate">
        <el-date-picker v-model="queryParams.enterDate" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="采购人" prop="enterName">
        <el-input v-model="queryParams.enterName" placeholder="请输入报账人" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
     <!-- <el-form-item label="报账人电话" prop="enterPhone">
        <el-input v-model="queryParams.enterPhone" placeholder="请输入报账人电话" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item label="采购类目" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.TEAM_LEDGER_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
     <el-form-item label="审核状态" prop="approvalStatus">
       <el-select v-model="queryParams.approvalStatus" placeholder="请选择审核状态" clearable size="small">
         <el-option v-for="dict in this.getDictDatas(DICT_TYPE.TEAM_LEDGER_APPROVAL_STATUS)"
                      :key="dict.value" :label="dict.label" :value="dict.value"/>
       </el-select>
	 </el-form-item>
    <!--  <el-form-item label="单价" prop="unitPrice">
        <el-input v-model="queryParams.unitPrice" placeholder="请输入单价" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="报账金额,单位为分" prop="money">
        <el-input v-model="queryParams.money" placeholder="请输入报账金额,单位为分" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> 
      <el-form-item label="审核状态" prop="approvalStatus">
        <el-select v-model="queryParams.approvalStatus" placeholder="请选择审核状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.TEAM_LEDGER_APPROVAL_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>-->
      <!--<el-form-item label="审核人" prop="approvalName">
        <el-input v-model="queryParams.approvalName" placeholder="请输入审核人" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="审核备注" prop="approvalRemarks">
        <el-input v-model="queryParams.approvalRemarks" placeholder="请输入审核备注" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="queryParams.remarks" placeholder="请输入备注" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
       <el-form-item label="自定义" prop="column1">
        <el-input v-model="queryParams.column1" placeholder="请输入自定义" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="自定义" prop="column2">
        <el-input v-model="queryParams.column2" placeholder="请输入自定义" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="自定义" prop="column3">
        <el-input v-model="queryParams.column3" placeholder="请输入自定义" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="自定义" prop="column4">
        <el-input v-model="queryParams.column4" placeholder="请输入自定义" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="自定义" prop="column5">
        <el-input v-model="queryParams.column5" placeholder="请输入自定义" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="自定义" prop="column6">
        <el-input v-model="queryParams.column6" placeholder="请输入自定义" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> 
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"
                   v-hasPermi="['team:ledger:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['team:ledger:export']">导出</el-button>
      </el-col>
          <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="isEmpty(checkedIds)"
            @click="handleDeleteBatch"
            v-hasPermi="['team:ledger:delete']"
        >
          批量删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

            <el-table
          v-loading="loading"
          :data="list"
          :stripe="true"
		  border
         
          @selection-change="handleRowCheckboxChange"
      >
        <el-table-column type="selection" width="55" />
        <!--    <el-table-column label="账本id" align="center" prop="id" /> -->
      <el-table-column label="采购时间" align="center" prop="enterDate" width="120">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.enterDate,'{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="采购人" align="center" prop="enterName" width="80"/>
      <!-- <el-table-column label="报账人电话" align="center" prop="enterPhone" /> -->
      <el-table-column label="采购类目" align="center" prop="type">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.TEAM_LEDGER_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="单价" align="center" prop="unitPrice" width="60">
		    <template v-slot="scope">
		  		<span>{{(scope.row.unitPrice/100).toFixed(2)}}</span>   
		  	</template>
	  </el-table-column>
	  <el-table-column label="数量/斤/个" align="center" prop="column1"width="100" />
      <el-table-column label="小计" align="center" prop="money"width="100">
		<template v-slot="scope">
			<span>{{(scope.row.money/100).toFixed(2)}}</span>   
		</template>  
	  </el-table-column>
	  <el-table-column label="上传凭证" align="center" prop="url">
	  		  <template v-slot="scope">
	  		     <image-preview  :src="scope.row.url"  :width="'100px'"></image-preview>
	  		  </template>
	  </el-table-column>
      <el-table-column label="审核状态" align="center" prop="approvalStatus"width="80">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.TEAM_LEDGER_APPROVAL_STATUS" :value="scope.row.approvalStatus" />
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="approvalName"width="80" />
      <el-table-column label="审核备注" align="center" prop="approvalRemarks" />
	  <el-table-column label="审核时间" align="center" prop="column2" />
      <el-table-column label="备注" align="center" prop="remarks" />
     <!-- <el-table-column label="自定义" align="center" prop="column1" />
      <el-table-column label="自定义" align="center" prop="column2" />
      <el-table-column label="自定义" align="center" prop="column3" />
      <el-table-column label="自定义" align="center" prop="column4" />
      <el-table-column label="自定义" align="center" prop="column5" />
      <el-table-column label="自定义" align="center" prop="column6" /> 
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
			<el-button size="mini" type="text" icon="el-icon-delete" @click="clickAppl(scope.row)"
			           v-hasPermi="['team:ledger:clickAppl']">审核</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"
		  
                     v-hasPermi="['team:ledger:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
               v-if="!scope.row.approvalName"      v-hasPermi="['team:ledger:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
	<el-dialog title="审核" :visible.sync="DialogAppl" width="30%" >
	  <div>
		  <el-form  :model="formData"  label-width="100px">
		    <el-form-item label="审核备注" >
		      <el-input type="textarea" v-model="formData.approvalRemarks" placeholder="请输入" />
		    </el-form-item>
		  </el-form>				
	  </div>
	  <span slot="footer" class="dialog-footer">
		<el-button type="primary" @click="submitAppl(2)">通过</el-button>
	    <el-button type="warning" @click="submitAppl(3)">不通过</el-button>
	  </span>
	</el-dialog>
				
    <!-- 对话框(添加 / 修改) -->
    <LedgerForm ref="formRef" @success="getList" />
    </div>
</template>

<script>
import ImageUpload from '@/components/ImageUpload';
import * as LedgerApi from '@/api/team/ledger';
import LedgerForm from './LedgerForm.vue';
import ImagePreview from "@/components/ImagePreview";
import { mapGetters } from 'vuex'
import moment from 'moment';
export default {
  name: "Ledger",
  components: {
          LedgerForm,ImageUpload,ImagePreview
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
	  DialogAppl:false,
      // 显示搜索条件
      showSearch: true,
              // 总条数
        total: 0,
      // 报账管理列表
      list: [],
	  formData:{
		   remarks: undefined,
	  },
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      checkedIds: [],
      // 查询参数
      queryParams: {
                    pageNo: 1,
            pageSize: 10,
        enterDate: [],
        enterName: null,
        enterPhone: null,
        type: null,
        productName: null,
        unitPrice: null,
        money: null,
        approvalStatus: null,
        approvalName: null,
        approvalRemarks: null,
        url: null,
        remarks: null,
        column1: null,
        column2: null,
        column3: null,
        column4: null,
        column5: null,
        column6: null,
        createTime: [],
      },
            };
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'nickname',
      'device'
    ]),
  },	
  methods: {
    /** 查询列表 */
    async getList() {
      try {
      this.loading = true;
              const res = await LedgerApi.getLedgerPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
	//审核
	clickAppl(row){
		this.formData=row
		this.DialogAppl=true
	},
	//提交审核 
	async submitAppl(type){
		this.formData.approvalStatus=type;
		this.formData.approvalName=this.nickname
		this.formData.column2 = moment().format('YYYY-MM-DD HH:mm');
		try {
			if (this.formData.id) {
				console.log("提交审核数据",this.formData)
				await LedgerApi.updateLedger(this.formData);
				this.$modal.msgSuccess("提交成功");
				this.DialogAppl = false;
				return;
			}	
		}finally {
			this.getList()
        }
		
		
	},
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除报账管理编号为"' + id + '"的数据项?')
      try {
       await LedgerApi.deleteLedger(id);
       await this.getList();
       this.$modal.msgSuccess("删除成功");
      } catch {}
    },
    /** 批量删除报账管理 */
    async handleDeleteBatch() {
      await this.$modal.confirm('是否确认删除?')
      try {
        await LedgerApi.deleteLedgerList(this.checkedIds);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {}
    },
    handleRowCheckboxChange(records) {
      this.checkedIds = records.map((item) => item.id);
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有报账管理数据项?');
      try {
        this.exportLoading = true;
        const data = await LedgerApi.exportLedgerExcel(this.queryParams);
        this.$download.excel(data, '报账管理.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
              }
};
</script>
